﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region RPTTemperatureAndHumidity

        public List<tb_RPT_TemperatureAndHumidity> GetAllTemperatureAndHumidity()
        {
            var query = (from obj in this.tb_RPT_TemperatureAndHumidities
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }


        public List<tb_RPT_TemperatureAndHumidity> GetTemperatureAndHumidityByContition(string strRecordDate, string strRecordTime
                                                                                    , string strTemperature, string strHumidity, string strRecorder)
        {
            var tq = from obj in this.tb_RPT_TemperatureAndHumidities
                     where obj.IsDelete == 0
                     select obj;

            if (strRecordDate.Length > 0)
            {
                tq = tq.Where(entity => entity.RecordDate == strRecordDate);
            }

            if (strRecordTime.Length > 0)
            {
                tq = tq.Where(entity => entity.RecordTime == strRecordTime);
            }
            if (strTemperature.Length > 0)
            {
                tq = tq.Where(entity => entity.Temperature == strTemperature);
            }
            if (strHumidity.Length > 0)
            {
                tq = tq.Where(entity => entity.Humidity == strHumidity);
            }

            if (strRecorder.Length > 0)
            {
                tq = tq.Where(entity => entity.Recorder == strRecorder);
            }

            var query = tq.ToList();
            return query;
        }

        public tb_RPT_TemperatureAndHumidity GetTemperatureAndHumidityById(int id)
        {
            return this.tb_RPT_TemperatureAndHumidities.SingleOrDefault(tb_RPT_TemperatureAndHumidities => tb_RPT_TemperatureAndHumidities.Id == id);
        }

        public void CreateTemperatureAndHumidity(tb_RPT_TemperatureAndHumidity entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_RPT_TemperatureAndHumidities.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyTemperatureAndHumidity(tb_RPT_TemperatureAndHumidity entity)
        {
            tb_RPT_TemperatureAndHumidity instance = this.tb_RPT_TemperatureAndHumidities.SingleOrDefault(p => p.Id == entity.Id);
            instance.RecordDate = entity.RecordDate;
            instance.RecordTime = entity.RecordTime;
            instance.Recorder = entity.Recorder;
            instance.Temperature = entity.Temperature;
            instance.Humidity = entity.Humidity; 

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;

            this.SubmitChanges();
        }

        public void DeleteTemperatureAndHumidity(tb_RPT_TemperatureAndHumidity entity)
        {
            tb_RPT_TemperatureAndHumidity deleteEntity = tb_RPT_TemperatureAndHumidities.SingleOrDefault(p => p.Id == entity.Id);
            tb_RPT_TemperatureAndHumidities.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

        #endregion

    }
}
